﻿Imports System.Data.Linq
Imports TNCDAL
Imports HTLibrary
Public Class ucLineCont
    Private lineContCtrl As New CLineContController
    Private lineCtrl As New CLine
    Private _curLineCont As tblLineCont = Nothing
    Private _curLineID As Integer = -1
#Region "ListView"
    Private Sub lvRoles_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvLines.SelectedIndexChanged
        cboContTypeSearch.SelectedIndex = -1
        txtConNoSearch.Text = ""
        If (lvLines.SelectedItems.Count > 0) Then
            Dim item As ListViewItem = lvLines.SelectedItems(0)
            _curLineID = CInt(item.SubItems(0).Text) 'lay ma hãng tàu
            HienThiGridConts(_curLineID, -1, "", "List containers in " & item.SubItems(1).Text)
        End If
    End Sub
#End Region

#Region "Gridview"
    Private Sub dgUsers_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgConts.SelectionChanged
        Try
            'hiển thị thông tin người dùng
            Dim _id = dgConts.CurrentRow.Cells("ID_LineCont").Value
            _curLineCont = lineContCtrl.SelectRecordByID(_id)
            BindDataToTextBox()
        Catch ex As Exception
        End Try
    End Sub
#End Region

#Region "Buttons"
    Private Sub btSearchAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSearchAll.Click
        HienThiGridConts(-1, cboContTypeSearch.SelectedValue, txtConNoSearch.Text, "List containers of search result")
    End Sub
    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        CViewer.BindContTypeToComboBox(cboContType)
        CViewer.BindLinesToComboBox(cboLines)
        CViewer.BindContTypeToComboBox(cboContTypeSearch)
        HienThiListView()
    End Sub
    Private Sub btXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        'lay ra ID cua user hien tai
        If (_curLineCont IsNot Nothing) Then
            If (MessageBox.Show("Có phải bạn muốn xoá container:" + _curLineCont.ContNo + " ?", "Xác nhận", MessageBoxButtons.YesNo) = vbYes) Then
                _curLineCont.IsActive = False
                lineContCtrl.UpdateRecordWithoutAttach(_curLineCont)
                'hien thi lai datagridview
                HienThiGridConts(_curLineID, -1, "", "List containers in " & _curLineCont.tblLine.LineName)
                HienThiTrangThai_Thuong()
            End If
        End If

    End Sub

    Private Sub btThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        'gọi form User
        XoaTrangTextBox()
        HienThiTrangThai_Sua()
        'Init obj
        _curLineCont = New tblLineCont
        _curLineCont.ID_LineCont = -1
        cboContType.Focus()
    End Sub
    Private Sub btSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'lay ra ID cua user hien tai
        HienThiTrangThai_Sua()
        cboContType.Focus()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Dim _id As Integer = dgConts.CurrentRow.Cells("ID_LineCont").Value
            _curLineCont = lineContCtrl.SelectRecordByID(_id)
        Catch ex As Exception
            _curLineCont = Nothing
        End Try
        BindDataToTextBox()
        HienThiTrangThai_Thuong()
    End Sub
    Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        ThemLineCont()
    End Sub
    Private Sub btnUpdateAndNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdateNew.Click
        If (ThemLineCont() = True) Then
            btThem_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "Utilities Function, Sub"
    Private Sub MyTextBox_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles txtContNo.Enter, txtConNoSearch.Enter
        ' Kick off SelectAll asyncronously so that it occurs after Click
        Dim txt As TextBox = CType(sender, TextBox)
        BeginInvoke(DirectCast(Sub() txt.SelectAll(), Action))
    End Sub
    Private Sub HienThiListView()
        'xoa het may cai cu
        lvLines.Items.Clear()
        'lay danh sach cac role
        Dim lines = lineCtrl.SelectAllRecord()
        For Each tbl In lines
            Dim item As New ListViewItem(tbl.ID_Line)
            item.SubItems.Add(tbl.LineName)
            'item.Text = tbl.RoleName
            lvLines.Items.Add(item)
        Next
        If (lvLines.Items.Count > 0) Then
            lvLines.Items(0).Selected = True
        End If
    End Sub
    Public Sub HienThiGridConts(ByVal idLine As Integer, ByVal idContType As Integer, ByVal sContNo As String, ByVal title As String)
        HienThiTrangThai_Thuong()
        XoaTrangTextBox()
        'lay danh sach nguoi su dung theo nhom quyen
        Dim listConts = lineContCtrl.SelectContsByLine(idLine, idContType, sContNo)
        Dim source As New BindingSource
        source.DataSource = listConts
        BindingNavigator1.BindingSource = source
        dgConts.DataSource = source
        dgConts.Columns("ID_LineCont").Visible = False
        dgConts.Columns("ContNo").Width = 100
        dgConts.Columns("ContNo").HeaderText = "Cont No"
        dgConts.Columns("ContTypeName").HeaderText = "Cont type"
        panelLineCont.TitleText = title
        If (dgConts.Rows.Count = 0) Then
            btnEdit.Enabled = False
            btnDel.Enabled = False
            txtContNo.Text = ""
        Else
            btnEdit.Enabled = True
            btnDel.Enabled = True
        End If
    End Sub
    Private Sub HienThiTrangThai_Thuong()
        dgConts.Enabled = True
        btnAdd.Enabled = True
        btnDel.Enabled = True
        btnEdit.Enabled = True

        btnUpdate.Enabled = False
        btnUpdateNew.Enabled = False
        btnCancel.Enabled = False

        cboContType.Enabled = False
        cboLines.Enabled = False
        txtContNo.Enabled = False
    End Sub
    Private Sub HienThiTrangThai_Sua()
        dgConts.Enabled = False
        btnAdd.Enabled = False
        btnDel.Enabled = False
        btnEdit.Enabled = False

        btnUpdate.Enabled = True
        btnUpdateNew.Enabled = True
        btnCancel.Enabled = True

        cboContType.Enabled = True
        cboLines.Enabled = True
        txtContNo.Enabled = True
    End Sub
    Private Sub XoaTrangTextBox()
        lblContNoError.Visible = False
        _curLineCont = Nothing
        txtContNo.Text = ""
        If (cboContType.Items.Count > 0) Then
            cboContType.SelectedIndex = 0
        Else
            MessageBox.Show("Chưa có thông tin Cont Type!")
            HienThiTrangThai_Thuong()
        End If
        If (cboLines.Items.Count > 0) Then
            If (_curLineID > 0) Then
                cboLines.SelectedValue = _curLineID
            Else
                cboLines.SelectedIndex = 0
            End If

        Else
            MessageBox.Show("Chưa có thông tin hãng tàu!")
            HienThiTrangThai_Thuong()
        End If
    End Sub
    Public Function ThemLineCont() As Boolean
        If (txtContNo.Text = "") Then
            lblContNoError.Visible = True
            txtContNo.Focus()
            Return False
        End If
        'kiểm tra có nhập đúng 11 ký tự
        If (txtContNo.TextLength <> 11) Then
            MessageBox.Show("Bạn phải nhập đúng 11 ký tự!")
            txtContNo.Focus()
            Return False
        End If
        'check contNo co ton tai hay chua
        If (lineContCtrl.CheckExists(txtContNo.Text, _curLineCont.ID_LineCont, _curLineID)) Then
            MessageBox.Show("Cont No đã tồn tại!")
            lblContNoError.Visible = True
            txtContNo.Focus()
            Return False
        End If
        lblContNoError.Visible = False
        With _curLineCont
            .ContNo = txtContNo.Text
            .ID_Line = cboLines.SelectedValue
            .ID_ContType = cboContType.SelectedValue
            .IsActive = True
        End With
        If (_curLineCont.ID_LineCont <> -1) Then
            lineContCtrl.UpdateRecordWithoutAttach(_curLineCont)
        Else
            lineContCtrl.InsertRecord(_curLineCont)
        End If
        HienThiGridConts(cboLines.SelectedValue, -1, "", "List containers in " & cboLines.Text)
        HienThiTrangThai_Thuong()
        Return True
    End Function
    Private Sub BindDataToTextBox()
        lblContNoError.Visible = False
        If (_curLineCont IsNot Nothing) Then
            'hien thi ra cac textbox
            txtContNo.Text = _curLineCont.ContNo
            cboContType.SelectedValue = _curLineCont.ID_ContType
            cboLines.SelectedValue = _curLineCont.ID_Line
        Else
            XoaTrangTextBox()
        End If

    End Sub
#End Region

    Private Sub ucManageUsers_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CViewer.BindContTypeToComboBox(cboContType)
        CViewer.BindLinesToComboBox(cboLines)
        CViewer.BindContTypeToComboBox(cboContTypeSearch)
        HienThiListView()
        HienThiTrangThai_Thuong()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        DataGridViewHelper.DataGridView_InitEvent(dgConts)
    End Sub

    Private Sub panelThongTin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles panelThongTin.Click

    End Sub
End Class
